import requests
import asyncio
import time
import aiohttp


async def test1():
    start = time.time()
    print('text1 begin')
    await asyncio.sleep(3)
    print('text1 end', time.time() - start)


async def test2():
    start = time.time()
    print('text2 begin')
    await asyncio.sleep(5)
    print('text2 end', time.time() - start)


async def main_test():
    await asyncio.create_task(test1())
    await asyncio.create_task(test2())


# asyncio.run(main_test())


async def fun1(num):
    print(num, 'start')
    await asyncio.sleep(1)
    print(num, 'end')


async def fun2(num):
    print(num, 'start2')
    await asyncio.sleep(3)
    print(num, 'end2')


async def main():
    await asyncio.gather(fun1(1), fun2(2))


# asyncio.run(main())


async def fun3(num):
    print(num, 'start3')
    await asyncio.sleep(1)
    print(num, 'end3')


async def main2():
    fun = [fun3(num) for num in range(10)]
    await asyncio.gather(*fun)


# asyncio.run(main2())


async def main3():
    async with aiohttp.ClientSession() as session:
        async with session.get(url='http://httpbin.org/get?age=1') as response:
            response = await response.json()
            # print(response)


# asyncio.run(main3())



async def fetch(session, url):
    async with session.get(url) as response:
        response = await response.json()
        return response


async def main4():
    async with aiohttp.ClientSession() as session:
        result = await asyncio.gather(*[fetch(session, f'http://httpbin.org/get?age={page}') for page in range(10)])
        for r in result:
            print(r)


start = time.time()
asyncio.run(main4())
print(time.time() - start)
